<?php
/**
 * 重庆柯一网络有限公司
 * 遵循MT协议，开源并可商业使用，没有任何限制
 * @Author:cqkyi
 * @Date: 2020/9/13 21:25
 * 官方惟一地址：www.cqkyi.com
 */

namespace app\apiadmin\controller;


use app\common\model\admin\MenuModel;
use app\common\model\admin\UserModel;
use app\common\model\admin\UserRoleModel;

use app\common\utils\Res;

class User extends AuthController
{

    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserInfo(){
        $roleresult = UserRoleModel::findByUserRoleId($this->request->user_id);
        $roleIds=[];
        foreach ($roleresult as $k=>$v){
            $roleIds[]=$v['role_id'];
        }
        if(in_array(1,$roleIds)){
            $result[0] =['perms'=>'*']; // MenuModel::adminPerms();
        }else{
            $result = MenuModel::userPermsByUserName($this->request->username);
        }
        $node=[];
        foreach ($result as $k=>$v){
            if($v['perms']){
                array_push($node,$v['perms']);

            }
        }

        $data['perms']=$node;
        $data['userInfo']=['username'=>$this->request->username,'avatar'=>$this->request->avatar];
        return Res::ok("查询成功",$data);
    }

    public function logout(){
        return Res::ok("退出成功",'');
    }


    public function index(){
        $param = $this->request->post();
        $limit =(int)$param['pageSize'];
        $offset = ($param['pageNumber'] - 1) * $limit;
        $keyword = $param['keyword'];
        $map=[];
        if($keyword){
            $map[] = ['username','like','%'.$keyword.'%'];
        }
        $res =UserModel::pageMap($map, $offset, $limit);
        $total = UserModel::pageMapCount($map);
        $data=[
            'list'=>$res,
            'total'=>$total
        ];
        return Res::ok("查询成功",$data);
    }


    public function add(){
        $param = $this->request->post();
        $rules = $this->request->post('role/a');
        $param['create_by']=$this->request->username;
        $param['password']=md5($param['password']);
        $param['create_time']=date('Y-m-d H:i:s',time());
        $result = UserModel::add($param,$rules);
        return Res::jsonResult($result);
    }


    public function edit(){
        $param = $this->request->post();
        $rules = $this->request->post('role/a');
        $param['update_by']=$this->request->username;
        $param['update_time']=date('Y-m-d H:i:s',time());
        $result = UserModel::edit($param,$rules);
        return Res::jsonResult($result);
    }


    public function findUserRole($id){
        //查询角色
       $result =UserRoleModel::findByUserRoleId($id);
        $data=[];
        foreach ($result as $k=>$v){
            $data[$k]=$v['role_id'];
        }
        return Res::ok("查询成功",$data);
    }

    public function del($id){
        $result = UserModel::del($id);
        return Res::jsonResult($result);
    }

    public function batchRemove(){
        $param = $this->request->post("ids/a");
        $result = UserModel::batchRemove($param);
        return Res::jsonResult($result);
    }

}